/**
 * Created by Julia on 2016/4/28.
 */
define(['jquery', 'domReady!', 'appConst', 'business/role/roleConfig', 'appMessageBox', 'jquery-form/jquery-form.min'], function ($, domReady, appConst, roleConfig, msgBox) {
    return {
        initEditRole: function () {
            if ("Administrator" == $("#name").val()) {
                $("#able_div").attr("style", "display: none");
            } else {
                $("#able_div").attr("style", "display: block");
            }

            $("#role-form").validate({
                rules:roleConfig.rules,
                messages:roleConfig.messages,
                submitHandler:function(form){
                    $(form).ajaxSubmit({
                        success:function(d){
                            if (d.code == 0) {
                                msgBox.errorBox(d.message, "MENU_SYSTEM_ROLE");
                               /* $.smallBox({
                                    title: $.i18n.prop("MESSAGEBOX_TITLE_FAIL"),
                                    content: $.i18n.prop(d.message, [$.i18n.prop("MENU_SYSTEM_ROLE")]),
                                    color: appConst.danger,
                                    timeout: appConst.timeout,
                                    icon: "fa fa-bell"
                                });*/
                                return false;
                            }

                            $("#role-form").addClass('submited');
                        },
                        error: function(e){
                            if (e.status == 401) {
                                msgBox.sessionFailureBox();
                            }
                            if (e.status == 500) {
                                msgBox.serverFailureBox();
                            }
                            if (e.status == 404) {
                                msgBox.pathFailureBox();
                            }
                        }
                    });
                },
                errorPlacement:function (error,element) {
                    error.insertAfter(element.parent());
                }
            });
        },
        initEvent: function () {
            var enabled = $('#enabled').val();
            $('.r-enabled').each(function () {
                var enabled02 = $(this).attr('data-value');
                if (enabled02 == enabled) {
                    $(this).addClass($(this).attr('data-enabled'));
                }
            });

            $('.r-enabled').click(function(e) {
                $('.r-enabled').each(function (){
                    $(this).removeClass($(this).attr('data-enabled'));
                });
                $(this).addClass($(this).attr('data-enabled'));
                $('#enabled').val($(this).attr('data-value'));
            });

            $('#lk_addnew').click(function(){
                $('#role-form').removeClass('submited');
                $('#role-form')[0].reset();
                e.preventDefault();
            });
        }
    }
});

